import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { FactoryToyItem, FactoryToyItemId } from './FactoryToyItem';

export interface FactoryBaseAttributes {
  id: string;
  factoryName: string;
  typeCode?: string;
  regionCode?: string;
  address?: string;
  contactPerson?: string;
  contactPhone?: string;
  email?: string;
  status?: number;
  remark?: string;
  createdBy?: string;
  updatedBy?: string;
  createdTime: Date;
  updatedTime: Date;
}

export type FactoryBasePk = 'id';
export type FactoryBaseId = FactoryBase[FactoryBasePk];
export type FactoryBaseOptionalAttributes =
  | 'typeCode'
  | 'regionCode'
  | 'address'
  | 'contactPerson'
  | 'contactPhone'
  | 'email'
  | 'status'
  | 'remark'
  | 'createdBy'
  | 'updatedBy'
  | 'createdTime'
  | 'updatedTime';
export type FactoryBaseCreationAttributes = Optional<
  FactoryBaseAttributes,
  FactoryBaseOptionalAttributes
>;

export class FactoryBase
  extends Model<FactoryBaseAttributes, FactoryBaseCreationAttributes>
  implements FactoryBaseAttributes
{
  id!: string;
  factoryName!: string;
  typeCode?: string;
  regionCode?: string;
  address?: string;
  contactPerson?: string;
  contactPhone?: string;
  email?: string;
  status?: number;
  remark?: string;
  createdBy?: string;
  updatedBy?: string;
  createdTime!: Date;
  updatedTime!: Date;

  // FactoryBase hasMany FactoryToyItem via factoryId
  factoryToyItems!: FactoryToyItem[];
  getFactoryToyItems!: Sequelize.HasManyGetAssociationsMixin<FactoryToyItem>;
  setFactoryToyItems!: Sequelize.HasManySetAssociationsMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  addFactoryToyItem!: Sequelize.HasManyAddAssociationMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  addFactoryToyItems!: Sequelize.HasManyAddAssociationsMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  createFactoryToyItem!: Sequelize.HasManyCreateAssociationMixin<FactoryToyItem>;
  removeFactoryToyItem!: Sequelize.HasManyRemoveAssociationMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  removeFactoryToyItems!: Sequelize.HasManyRemoveAssociationsMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  hasFactoryToyItem!: Sequelize.HasManyHasAssociationMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  hasFactoryToyItems!: Sequelize.HasManyHasAssociationsMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  countFactoryToyItems!: Sequelize.HasManyCountAssociationsMixin;

  static initModel(sequelize: Sequelize.Sequelize): typeof FactoryBase {
    return FactoryBase.init(
      {
        id: {
          type: DataTypes.STRING(36),
          allowNull: false,
          primaryKey: true,
          comment: '主键ID（雪花字符串ID）',
        },
        factoryName: {
          type: DataTypes.STRING(128),
          allowNull: false,
          comment: '工厂名称',
          field: 'factory_name',
        },
        typeCode: {
          type: DataTypes.STRING(64),
          allowNull: true,
          comment: '工厂类型编码（如总部、分厂）',
          field: 'type_code',
        },
        regionCode: {
          type: DataTypes.STRING(64),
          allowNull: true,
          comment: '地区编码（省市区）',
          field: 'region_code',
        },
        address: {
          type: DataTypes.STRING(255),
          allowNull: true,
          comment: '详细地址',
        },
        contactPerson: {
          type: DataTypes.STRING(64),
          allowNull: true,
          comment: '联系人',
          field: 'contact_person',
        },
        contactPhone: {
          type: DataTypes.STRING(32),
          allowNull: true,
          comment: '联系电话',
          field: 'contact_phone',
        },
        email: {
          type: DataTypes.STRING(128),
          allowNull: true,
          comment: '邮箱',
        },
        status: {
          type: DataTypes.TINYINT,
          allowNull: true,
          defaultValue: 1,
          comment: '状态（0：停用，1：启用）',
        },
        remark: {
          type: DataTypes.TEXT,
          allowNull: true,
          comment: '备注信息',
        },
        createdBy: {
          type: DataTypes.STRING(32),
          allowNull: true,
          comment: '创建人ID',
          field: 'created_by',
        },
        updatedBy: {
          type: DataTypes.STRING(32),
          allowNull: true,
          comment: '更新人ID',
          field: 'updated_by',
        },
        createdTime: {
          type: DataTypes.DATE,
          allowNull: false,
          defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
          comment: '创建时间',
          field: 'created_time',
        },
        updatedTime: {
          type: DataTypes.DATE,
          allowNull: false,
          defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
          comment: '更新时间',
          field: 'updated_time',
        },
      },
      {
        sequelize,
        tableName: 'factory_base',
        timestamps: false,
        indexes: [
          {
            name: 'PRIMARY',
            unique: true,
            using: 'BTREE',
            fields: [{ name: 'id' }],
          },
          {
            name: 'idx_factory_base',
            using: 'BTREE',
            fields: [{ name: 'id' }],
          },
        ],
      },
    );
  }
}
